package we.caro;

public class CaroGameLogic {


    private final int TABLE_SIZE = 15;
    private final int BLACK = 1;
    private final int WHITE = 2;
    private final int EMPTY = 0;

    private int currentPlayer;
    private int[][] board = new int[TABLE_SIZE][TABLE_SIZE];
    int win_r1, win_c1, win_r2, win_c2;
    int number_move = 0;

    public CaroGameLogic() {
        super();
        reset();
    }

    public void reset() {
        for (int i = 0; i < TABLE_SIZE; i++)
            for (int j = 0; j < TABLE_SIZE; j++) {
                board[i][j] = EMPTY;
            }
        currentPlayer = BLACK;
    }

    public void move(int place) {

        int row = place / TABLE_SIZE;
        int column = place % TABLE_SIZE;
        if (board[row][column] == EMPTY) {
            board[row][column] = currentPlayer;
            number_move++;
            if (currentPlayer == BLACK)
                currentPlayer = WHITE;
            else
                currentPlayer = BLACK;
        }
    }

    public boolean draw() {
        return number_move >= TABLE_SIZE * TABLE_SIZE;
    }

    /**
     *
     * @param row
     * @param col
     * @return -1: tran dau chua ket thuc
     *          0: tran dau hoa
     *          1: player 1 thang
     *          2: player 2 thang
     */
    private int result(int row, int col) {
        // This is called just after a piece has been played on the
        // square in the specified row and column. It determines
        // whether that was a winning move by counting the number
        // of squares in a line in each of the four possible
        // directions from (row,col). If there are 5 squares (or more)
        // in a row in any direction, then the game is won.

        if (win(board[row][col], row, col, 1, 0))
            return board[row][col];
        if (win(board[row][col], row, col, 0, 1))
            return board[row][col];
        if (win(board[row][col], row, col, 1, -1))
            return board[row][col];
        if (win(board[row][col], row, col, 1, 1))
            return board[row][col];
        /*
       * When we get to this point, we know that the game is not won. The
       * value of win_r1, which was changed in the count() method, has to be
       * reset to -1, to avoid drawing a red line on the board.
       */

        if (draw())
            return 0;

        win_r1 = -1;
        return -1; // tiep tuc

    } // end winner()

    private boolean win(int player, int row, int col, int dirX, int dirY) {
        // Counts the number of the specified player's pieces starting at
        // square (row,col) and extending along the direction specified by
        // (dirX,dirY). It is assumed that the player has a piece at
        // (row,col). This method looks at the squares (row + dirX, col+dirY),
        // (row + 2*dirX, col + 2*dirY), ... until it hits a square that is
        // off the board or is not occupied by one of the players pieces.
        // It counts the squares that are occupied by the player's pieces.
        // Furthermore, it sets (win_r1,win_c1) to mark last position where
        // it saw one of the player's pieces. Then, it looks in the
        // opposite direction, at squares (row - dirX, col-dirY),
        // (row - 2*dirX, col - 2*dirY), ... and does the same thing.
        // Except, this time it sets (win_r2,win_c2) to mark the last piece.
        // Note: The values of dirX and dirY must be 0, 1, or -1. At least
        // one of them must be non-zero.


        int ct = 1; // Number of pieces in a row belonging to the player.

        int r, c; // A row and column to be examined

        r = row + dirX; // Look at square in specified direction.
        c = col + dirY;
        while (r >= 0 && r < 13 && c >= 0 && c < 13 && board[r][c] == player) {
            // Square is on the board and contains one of the players's pieces.
            ct++;
            r += dirX; // Go on to next square in this direction.
            c += dirY;
        }

        win_r1 = r - dirX; // The next-to-last square looked at.
        win_c1 = c - dirY; // (The LAST one looked at was off the board or
        // did not contain one of the player's pieces.

        r = row - dirX; // Look in the opposite direction.
        c = col - dirY;
        while (r >= 0 && r < 13 && c >= 0 && c < 13 && board[r][c] == player) {
            // Square is on the board and contains one of the players's pieces.
            ct++;
            r -= dirX; // Go on to next square in this direction.
            c -= dirY;
        }

        win_r2 = r + dirX;
        win_c2 = c + dirY;

        // At this point, (win_r1,win_c1) and (win_r2,win_c2) mark the endpoints
        // of the line of pieces belonging to the player.

        return (ct == 5);

    } // end count()

}
